/**
 * com.future CO.,ltd.
 */

package com.future.mq;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * 类说明：消息接收
 */
public class Receiver {

    public static void main(String[] args) throws Exception {

        // 获取连接工厂
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
                ActiveMQConnectionFactory.DEFAULT_USER,
                ActiveMQConnectionFactory.DEFAULT_PASSWORD,
                "tcp://localhost:61616"
        );
        // hu获取一个向ActiveMQ的连接
        Connection connection = connectionFactory.createConnection();
        connection.start();
        // 获取sesseion
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //找到目的地，获取destination，消费端，也会从这个目的地取消息
        Queue queue = session.createQueue("user");
        // 获取消息
        MessageConsumer consumer = session.createConsumer(queue);
        while (true) {
            TextMessage receive = (TextMessage) consumer.receive();
            System.out.println("message:" + receive.getText());
        }

        // 关闭连接
        //connection.close();
        //System.out.println("介绍完毕!!");
    }
}
